<?php
namespace Home\Controller;
use Think\Controller;
class AccountController extends Controller {
	public function index(){

		if(isset($_SESSION['username'])){
     	    $this->assign('login','true');
         	$this->assign('username',$_SESSION['username']);
        }

		$this->display();
	}

    public function doLogin(){
    	$username = $_POST['username'];
    	$password = $_POST['password'];

    	$user = D('user');
    	$where['username'] = $username;
    	$where['password'] = $password;
    	$arr = $user->field('id')->where($where)->find();

    	if($arr){
            $_SESSION['username'] = $username;
            $_SESSION['id'] = $arr['id'];
    		$this->redirect('Index/index');
    	}else{
    		$this->error('登录失败');
    	}
    }	

     public function doLogout(){
        $_SESSION = array();
        if(isset($_COOKIE[session_name()])){
            setcookie(session_name(),'',time()-1,'/');
        }
        session_destroy();
        $this->redirect('Index/index');
    }

    public function checkname(){
        $username = $_POST['username'];
        $user = M('user');
        $where['username'] = $username;
        $count = $user->where($where)->count();
        if ($count) {
            echo 0;  
        }else {
            echo 1;  
        }
    }

     
}